<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>

    <style>
        .container {
            width: 100%;
            height: 600px;
        }

        .rotation {
            padding: 0;
            width: 500px;
            height: 300px;
            border: 1px solid black;
            margin: 100px auto;
            position: relative;
        }

        .rotation img {
            width: 100%;
            height: 100%;
            z-index: -1;
        }


        .arrow {
            width: 30px;
            height: 30px;
            background-color: rgba(0, 0, 0, 0.4);
            border-radius: 50%;
            z-index: 10;
            position: absolute;
            top: 50%;
            transform: translateY(-50%);
        }

        .arrow:hover {
            background-color: rgba(0, 0, 0, 0.8);
            cursor: pointer;
        }

        .left-arrow {
            left: 5%;
        }

        .left-arrow::after {
            display: block;
            content: "";

            width: 0;
            height: 0;

            border: 10px solid transparent;
            border-right-color: white;
            margin-top: 50%;
            transform: translateY(-50%);


        }


        .right-arrow {
            right: 5%;
        }

        .right-arrow::after {
            display: block;
            content: "";

            width: 0;
            height: 0;

            border: 10px solid transparent;

            border-left-color: white;
            margin-top: 50%;

            transform: translateX(50%) translateY(-50%);

        }

        .dot-list {

            position: absolute;
            bottom: 0;
            left: 50%;


            background-color: rgba(0, 0, 0, 0.4);
            margin: 0;
            padding: 0 20px;
            transform: translateX(-50%);

            list-style-type: none;
            color: white;
            z-index: 10;
            overflow: hidden;
        }



        .dot-list li {
            display: inline;
            cursor: pointer;
        }

        .dot-list .active {
            color: black;
        }
    </style>
</head>

<body>
    <div class="container">
        <!-- 轮播图区域 -->
        <div class="rotation">
            <img id="id-rotation-img" src="https://www.baidu.com/img/flexible/logo/pc/result.png" alt="">

            <!-- 左右箭头区域 -->
            <div class="arrow left-arrow" id="id-left-arrow"></div>
            <div class="arrow right-arrow" id="id-right-arrow"></div>

            <!-- 底部图片列表 -->
            <ul class="dot-list">
                <li class="active">●</li>
                <li>●</li>
                <li>●</li>
                <li>●</li>
                <li>●</li>
            </ul>

        </div>
    </div>

    <script>
        // 图片容器
        let eImgContaner = document.querySelector(".rotation");

        // 左箭头
        let eLeftArrow = document.getElementById("id-left-arrow");

        // 右箭头
        let eRightArrow = document.getElementById("id-right-arrow");

        // 图
        let eImg = document.getElementById("id-rotation-img");

        // 原点列表
        let eUl = document.querySelector(".dot-list")
        let eLiList;

        let imgArray = [
            'http://qinniu.douziqianduan.icu/wallpaper01.jpg',
            'http://qinniu.douziqianduan.icu/wallpaper02.jpg',
            'http://qinniu.douziqianduan.icu/wallpaper03.jpg',
            'http://qinniu.douziqianduan.icu/wallpaper04.jpg',
            'http://qinniu.douziqianduan.icu/wallpaper05.jpg',
        ]

        // 定时器
        let timer = null;

        // 当前图片的索引
        let currentIndex = 0;

        // 更换轮播图的时间间隔
        let wait = 3000;

        // 更换照片方法
        function changeImg() {
            console.log("更换图片方法被触发");

            // 更换图片
            eImg.src = imgArray[currentIndex];


            // 更新下方小圆点
            for (let i = 0; i < imgArray.length; i++) {
                eLiList[i].className = "";
            }

            eLiList[currentIndex].className = "active"
        }


        // 自动播放方法
        function autoPlay() {
            console.log("即将开始自动播放");
            timer = setInterval(function () {

                currentIndex++;
                currentIndex %= imgArray.length;
                changeImg();
            }, wait)
        }

        // 左按钮
        eLeftArrow.onclick = function () {
            currentIndex--;

            if (currentIndex == -1) {
                // 倒退到最后一张图
                currentIndex = imgArray.length - 1;
            }

            changeImg();

            // 重新开始计时
            clearInterval(timer);
            autoPlay();
        }

        // 右按钮
        eRightArrow.onclick = function () {
            currentIndex++;

            if (currentIndex == imgArray.length) {
                // 回到第一张图
                currentIndex = 0;
            }

            changeImg();

            // 重新开始计时
            clearInterval(timer);
            autoPlay();
        }

        // 初始化圆点列表方法
        function dotListInit() {
            // 注意 这里是 -1 因为已经有了一个li active在页面中
            // let eLi;

            // for (let i = 0; i < imgArray.length - 1; i++) {
            //     eLi = document.createElement('li');
            //     eUl.append(eLi);
            //     // eUl.innerHTML += "<li></li>";
            // }
            
            //点击小圆点跳转到对应的图片
            
            eLiList = document.querySelectorAll(".dot-list li")
            for (let i = 0; i < imgArray.length; i++) {

                eLiList[i].index = i;

                eLiList[i].onclick = function (event) {
                    currentIndex = this.index;
                    changeImg();

                    // 重新开始计时
                    clearInterval(timer);
                    autoPlay();
                }
            }
        }


        // 初始化圆点列表
        dotListInit();

        // 刚进入页面时,先用第一张把logo图替换掉
        changeImg();
        autoPlay();
    </script>
</body>

</html>